/*
 * Copyright (C) 2014 Saúl Díaz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.twotiger.and.util;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;

import com.twotiger.and.view.CircularProgressDrawable;

/**
 * Circular progress drawable demonstration
 *
 * @author Saul Diaz <sefford@gmail.com>
 */
public class AnimationUtils {

    // Views
    ImageView ivDrawable;
    private Context context;
    private CircularProgressDrawable drawable;

    public AnimationUtils(Context context, CircularProgressDrawable drawable) {
        super();
        this.context = context;
        this.drawable = drawable;
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (currentAnimation != null) {
                currentAnimation.cancel();
            }
            currentAnimation = prepareStyle1Animation();
            currentAnimation = prepareStyle2Animation();
            currentAnimation = prepareStyle3Animation(0);
            currentAnimation = preparePulseAnimation();

            currentAnimation.start();
        }
    };

    Animator currentAnimation;

    /**
     * This animation was intended to keep a pressed state of the Drawable
     *
     * @return Animation
     */
    public Animator preparePressedAnimation() {
        Animator animation = ObjectAnimator.ofFloat(drawable,
            CircularProgressDrawable.CIRCLE_SCALE_PROPERTY, drawable.getCircleScale(), 0.65f);
        animation.setDuration(120);
        return animation;
    }

    /**
     * This animation will make a pulse effect to the inner circle
     *
     * @return Animation
     */
    public Animator preparePulseAnimation() {
        AnimatorSet animation = new AnimatorSet();

        Animator firstBounce = ObjectAnimator.ofFloat(drawable,
            CircularProgressDrawable.CIRCLE_SCALE_PROPERTY, drawable.getCircleScale(), 1);
        firstBounce.setDuration(300);
        firstBounce.setInterpolator(new CycleInterpolator(1));
        Animator secondBounce = ObjectAnimator.ofFloat(drawable,
            CircularProgressDrawable.CIRCLE_SCALE_PROPERTY, 0.75f, 0.83f);
        secondBounce.setDuration(300);
        secondBounce.setInterpolator(new CycleInterpolator(1));
        Animator thirdBounce = ObjectAnimator.ofFloat(drawable,
            CircularProgressDrawable.CIRCLE_SCALE_PROPERTY, 0.75f, 0.80f);
        thirdBounce.setDuration(300);
        thirdBounce.setInterpolator(new CycleInterpolator(1));

        animation.playSequentially(firstBounce, secondBounce, thirdBounce);
        return animation;
    }

    /**
     * Style 1 animation will simulate a indeterminate loading while taking advantage of the inner
     * circle to provide a progress sense
     *
     * @return Animation
     */
    public Animator prepareStyle1Animation() {
        AnimatorSet animation = new AnimatorSet();

        final Animator indeterminateAnimation = ObjectAnimator.ofFloat(drawable,
            CircularProgressDrawable.PROGRESS_PROPERTY, 0, 3600);
        indeterminateAnimation.setDuration(3600);

        Animator innerCircleAnimation = ObjectAnimator.ofFloat(drawable,
            CircularProgressDrawable.CIRCLE_SCALE_PROPERTY, 0f, 0.75f);
        innerCircleAnimation.setDuration(3600);
        innerCircleAnimation.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                drawable.setIndeterminate(true);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                indeterminateAnimation.end();
                drawable.setIndeterminate(false);
                drawable.setProgress(0);
            }
        });

        animation.playTogether(innerCircleAnimation, indeterminateAnimation);
        return animation;
    }

    /**
     * Style 2 animation will fill the outer ring while applying a color effect from red to green
     *
     * @return Animation
     */
    @SuppressLint("InlinedApi")
    public Animator prepareStyle2Animation() {
        AnimatorSet animation = new AnimatorSet();

        ObjectAnimator progressAnimation = ObjectAnimator.ofFloat(drawable,
            CircularProgressDrawable.PROGRESS_PROPERTY, 0f, 1f);
        progressAnimation.setDuration(3600);
        progressAnimation.setInterpolator(new AccelerateDecelerateInterpolator());

        ObjectAnimator colorAnimator = ObjectAnimator.ofInt(drawable,
            CircularProgressDrawable.RING_COLOR_PROPERTY,
            context.getResources().getColor(android.R.color.holo_red_dark), context.getResources()
                .getColor(android.R.color.holo_green_light));
        colorAnimator.setEvaluator(new ArgbEvaluator());
        colorAnimator.setDuration(3600);

        animation.playTogether(progressAnimation, colorAnimator);
        return animation;
    }

    /**
     * Style 3 animation will turn a 3/4 animation with Anticipate/Overshoot interpolation to a
     * blank waiting - like state, wait for 2 seconds then return to the original state
     *
     * @return Animation
     */
    public Animator prepareStyle3Animation(float value) {
        AnimatorSet animation = new AnimatorSet();

        ObjectAnimator progressAnimation = ObjectAnimator.ofFloat(drawable,
            CircularProgressDrawable.PROGRESS_PROPERTY, 0.5f, 0.5f - value);
        progressAnimation.setDuration(1200);
        progressAnimation.setInterpolator(new AnticipateInterpolator());

        Animator innerCircleAnimation = ObjectAnimator.ofFloat(drawable,
            CircularProgressDrawable.CIRCLE_SCALE_PROPERTY, 0, 0f);
        innerCircleAnimation.setDuration(1200);
        innerCircleAnimation.setInterpolator(new AnticipateInterpolator());

        ObjectAnimator invertedProgress = ObjectAnimator.ofFloat(drawable,
            CircularProgressDrawable.PROGRESS_PROPERTY, 0.5f, 0.5f - value);
        invertedProgress.setDuration(1200);
        invertedProgress.setStartDelay(2000);
        invertedProgress.setInterpolator(new OvershootInterpolator());

        //        Animator invertedCircle = ObjectAnimator.ofFloat(drawable, CircularProgressDrawable.CIRCLE_SCALE_PROPERTY, 0f, value);
        //        invertedCircle.setDuration(1200);
        //        invertedCircle.setStartDelay(2000);
        //        invertedCircle.setInterpolator(new OvershootInterpolator());
        //        invertedCircle.setStartDelay(2000);
        animation.playTogether(progressAnimation, innerCircleAnimation, invertedProgress);
        return animation;
    }
    
    /**
     * 
     */
    public static void doTranslateAnimation(View view, final float rawXPre, final float rawYPre , final float rawX,final float rawY){
        //初始化  
        TranslateAnimation translateAnimation = new TranslateAnimation(rawXPre, rawX,rawYPre,rawY);  
        //设置动画时间           
        translateAnimation.setDuration(1000);  
        translateAnimation.setFillAfter(true);
        
        view.startAnimation(translateAnimation);
        translateAnimation.setAnimationListener(new AnimationListener() {
            
            @Override
            public void onAnimationStart(Animation animation) {
                System.out.println(rawXPre + "rawXPre");
                System.out.println(rawYPre + "rawYPre");
            }
            
            @Override
            public void onAnimationRepeat(Animation animation) {
                
            }
            
            @Override
            public void onAnimationEnd(Animation animation) {
                System.out.println(rawX + "rawX");
                System.out.println(rawY + "rawY");
            }
        });
    }
}
